package utils.okhttp;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class baseOKDemo {

    public static void main(String[] args) throws IOException {
        sendGet();
    }


    public static void sendGet() throws IOException {

        FileWriter  fw = new FileWriter("a.txt",true);

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        int pigsiz=1;
        Boolean hast=true;
        while (hast){
            Request request = new Request.Builder()
                    .url("https://www.315jiage.cn/memberViewPrices.aspx?act=ajax&cmd=get&p="+pigsiz+"&w=mid&k=&minStock=&maxStock=&rnd=916210&hot=1&noload=0")
                    .method("GET", null)
                    .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36")
                    .addHeader("accept", "application/json")
                    .addHeader("Content-Type", "application/json;charset=utf8")
                    .addHeader("Cookie", "__yjs_duid=1_779845dfb5a478969d219f220f29bc3c1622010851276; iwmsGid=0G21KCH169QAZVX7GWAZ; Hm_lvt_4cce664ec5d8326cc457ab09053c15b2=1651650542,1651715800,1652061575,1652078286; iwmsUser=%7Bver:4,id:1152513,name:'15548861732',psw:'19145F8C980B8E3E',admin:0,admPs:0,msg:0,sendOrder:0,mshopid:12057,mshopOrderQueue:0,mfactoryid:0,doctorid:0,pharmacistID:0,wx:1,lastSet:11757140%7D; Hm_lpvt_4cce664ec5d8326cc457ab09053c15b2=1652084438")
                    .build();
            Response response = client.newCall(request).execute();
            String line=response.body().string();
            fw.write(line+"\r\n");
            JSONObject jsonObject = JSON.parseObject(line);

            Integer success = (Integer)jsonObject.get("success");
            if (1==success) {
                JSONArray objs = jsonObject.getJSONArray("objs");
                List<Tlbaseinfo> list=new ArrayList<>();
                for (Object o:objs) {
                    Tlbaseinfo tlbaseinfo = JSON.parseObject(String.valueOf(o), Tlbaseinfo.class);
                    list.add(tlbaseinfo);
                }


            }


            Integer a=(Integer)jsonObject.get("haveMore");
            if (1!=a) {
                hast=false;
            }
            pigsiz++;
        }
        fw.flush();//清空缓冲区
        fw.close();//关闭输出流
    }
}
